package com.zzw.basic.classloader;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ExtendedObjectPoolFactory {

	private Map<String,Object> objectPool = new HashMap<String, Object>();
	private Properties config = new Properties();
	
	public void init(String fileName){
		try {
			FileInputStream fis = new FileInputStream(fileName);
			config.load(fis);
			System.out.println("???" + fileName + "??");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private Object createObject(String clazzName) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		Class<?> clazz = Class.forName(clazzName);
		return clazz.newInstance();
	}
	
	public void initPool() throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		for(String name:config.stringPropertyNames()){
			if(!name.contains("%")){
				objectPool.put(name, createObject(config.getProperty(name)));
			}
		}
	}
	
	public void initProperty() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		for(String name:config.stringPropertyNames()){
			if(name.contains("%")){
				String[] objAndProp = name.split("%");
				Object target = getObject(objAndProp[0]);
				String mtdName = "set" + objAndProp[1].substring(0, 1).toUpperCase()
						+ objAndProp[1].substring(1);
				Class<?> targetClass = target.getClass();
				Method mtd = targetClass.getMethod(mtdName, String.class);
				mtd.invoke(target, config.getProperty(name));
			}
		}
	}
	
	public Object getObject(String name){
		return objectPool.get(name);
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
		ExtendedObjectPoolFactory factory =  new ExtendedObjectPoolFactory();
		
		factory.init("D:\\WorkSpace\\ReflectDemo\\src\\main\\java\\com\\zzw\\classloader\\ext.txt");
		factory.initPool();
		factory.initProperty();
		System.out.println(factory.getObject("a"));
	}
}